//
//  MineViewController.m
//  Rail
//
//  Created by hychen on 2018/2/25.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "MineViewController.h"
#import "MineAboutViewController.h"
#import "UploadSettingViewController.h"
#import "UploadCenterViewController.h"
#import "UpdatePassWordViewController.h"
#import "ModifyUserInfoViewController.h"
#import "ObserverLineViewController.h"
#import "MineHeaderView.h"

#import "UINavigationController+FDFullscreenPopGesture.h"

#import "LoginViewController.h"
#import "ForgetPasswordViewController.h"

@interface MineViewController () <UINavigationControllerDelegate,UIAlertViewDelegate>
@property (nonatomic, strong) MineHeaderView *headerView;
@end

@implementation MineViewController

- (void)dealloc
{
    self.navigationController.delegate = nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setFd_prefersNavigationBarHidden:YES];
    
    self.view.backgroundColor = Color_Background;
    
    self.navigationController.delegate = self;
    
    [self addNotifications];
    
    [self setupTableHeaderView];
    
}

- (void)viewWillAppear:(BOOL)animated
{
//    [self.navigationController setNavigationBarHidden:YES animated:animated];
    [super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
//    [self.navigationController setNavigationBarHidden:NO animated:animated];
    [super viewWillDisappear:animated];
}

- (void)setupTableHeaderView {
    BMWeakSelf
    MineHeaderView *headerView = [MineHeaderView headerView];
    self.headerView = headerView;
    [headerView setBlockClickSetting:^{
        [weakSelf gotoSettingController];
    }];
    [headerView setBlockClickUserInfo:^{
        [weakSelf gotoUserInfoModifyController];
    }];
    headerView.userInfoModel = [GlobalManage sharedManage].userInfoModel;
    self.tableView.tableHeaderView = headerView;
    [self.tableView reloadData];
}

#pragma mark - 通知
- (void)addNotifications {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveUploadAvatarSuccess) name:Notify_Upload_Avatar_Success object:nil];
}

- (void)receiveUploadAvatarSuccess {
    [self setupTableHeaderView];
}

#pragma mark - 跳转
- (void)gotoSettingController
{
    UploadSettingViewController *setVc = [UploadSettingViewController new];
    setVc.hidesBottomBarWhenPushed = YES;
    [[UINavigationController rootNavigationController] pushViewController:setVc animated:YES];
}

- (void)gotoUserInfoModifyController {
    ModifyUserInfoViewController *vc = [ModifyUserInfoViewController new];
    [[UINavigationController rootNavigationController] pushViewController:vc animated:YES];
}

- (void)czh_setGroup
{
    [super czh_setGroup];
    
    BMWeakSelf
    CZHBadgeItem *collectionItem = [CZHBadgeItem itemWithImageName:@"me_collect_ic" title:@"我的收藏" canClick:YES haveLine:YES];
    collectionItem.operationBlock = ^(CZHBaseSettingItem *item, NSIndexPath *indexPath) {
        [weakSelf.tabBarController setSelectedIndex:2];
    };
    CZHSettingFrameModel *collectionFrameModel = [[CZHSettingFrameModel alloc] init];
    collectionFrameModel.titleFont = [UIFont systemFontOfSize:16];
    collectionFrameModel.cellHeight = 50;
    collectionFrameModel.bgColor = [UIColor whiteColor];
    collectionFrameModel.item = collectionItem;
    
    CZHBadgeItem *dailyItem = [CZHBadgeItem itemWithImageName:@"me_maintain_ic" title:@"日常维护" canClick:YES haveLine:YES];
    dailyItem.descVc = [ObserverLineViewController class];
    CZHSettingFrameModel *dailyFrameModel = [[CZHSettingFrameModel alloc] init];
    dailyFrameModel.titleFont = [UIFont systemFontOfSize:16];
    dailyFrameModel.cellHeight = 50;
    dailyFrameModel.bgColor = [UIColor whiteColor];
    dailyFrameModel.item = dailyItem;
    
//    CZHBadgeItem *uploadItem = [CZHBadgeItem itemWithImageName:@"me_uploading_ic" title:@"上传中心" canClick:YES haveLine:YES];
//    uploadItem.descVc = [UploadCenterViewController class];
//    CZHSettingFrameModel *uploadFrameModel = [[CZHSettingFrameModel alloc] init];
//    uploadFrameModel.titleFont = [UIFont systemFontOfSize:16];
//    uploadFrameModel.cellHeight = 50;
//    uploadFrameModel.bgColor = [UIColor whiteColor];
//    uploadFrameModel.item = uploadItem;
    
    CZHBadgeItem *pwdItem = [CZHBadgeItem itemWithImageName:@"me_password_edit_ic" title:@"修改密码" canClick:YES haveLine:YES];
    pwdItem.descVc = [UpdatePassWordViewController class];
    CZHSettingFrameModel *pwdFrameModel = [[CZHSettingFrameModel alloc] init];
    pwdFrameModel.titleFont = [UIFont systemFontOfSize:16];
    pwdFrameModel.cellHeight = 50;
    pwdFrameModel.bgColor = [UIColor whiteColor];
    pwdFrameModel.item = pwdItem;
    
    CZHBadgeItem *aboutUsItem = [CZHBadgeItem itemWithImageName:@"me_about_ic" title:@"关于我们" canClick:YES haveLine:YES];
    aboutUsItem.descVc = [MineAboutViewController class];
    CZHSettingFrameModel *aboutUsFrameModel = [[CZHSettingFrameModel alloc] init];
    aboutUsFrameModel.titleFont = [UIFont systemFontOfSize:16];
    aboutUsFrameModel.cellHeight = 50;
    aboutUsFrameModel.bgColor = [UIColor whiteColor];
    aboutUsFrameModel.item = aboutUsItem;
    
    CZHSectionItem *sectionZero = [[CZHSectionItem alloc] init];
    sectionZero.items = @[collectionFrameModel,dailyFrameModel,pwdFrameModel,aboutUsFrameModel];
    [self.sectionGroups addObject:sectionZero];
    
    CZHExitItem *exitItem = [CZHExitItem itemWithImageName:nil title:@"退出" canClick:YES];
    exitItem.operationBlock = ^(CZHBaseSettingItem *item, NSIndexPath *indexPath) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"是否确认退出当前账号?" message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
        [alertView show];
    };
    CZHSettingFrameModel *exitModel = [[CZHSettingFrameModel alloc] init];
    exitModel.cellHeight = 50;
    exitModel.item = exitItem;
    CZHSectionItem *sectionOne = [[CZHSectionItem alloc] init];
    sectionOne.headHeight = 50;
    sectionOne.items = @[exitModel];
    [self.sectionGroups addObject:sectionOne];
}

#pragma mark - UINavigationControllerDelegate
// 将要显示控制器
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 判断要显示的控制器是否是自己
    BOOL isShowHomePage = [viewController isKindOfClass:[self class]];
    
    [self.navigationController setNavigationBarHidden:isShowHomePage animated:NO];
}

#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    NSLog(@"buttonIndexClick:%ld",buttonIndex);
    if (buttonIndex == 1) {
        //点击退出
        LoginViewController *loginVc = [[LoginViewController alloc] initWithComeFrom:LoginComeFromTypeLogout];
        BaseNavigationController *nav = [[BaseNavigationController alloc] initWithRootViewController:loginVc];
        [UIApplication sharedApplication].delegate.window.rootViewController = nav;
    }
}
@end
